package com.the4man.framework.jdbc;

import com.the4man.framework.generate.FieldBean;

import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by lenovo on 2017/12/12.
 */
public class JDBCUtil {
    public JDBCUtil() {
    }

    public static Connection getConnection() {
        Connection conn = null;

        try {
            PropsUtil.init("/config.properties");
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(PropsUtil.getProp("jdbc_url"), PropsUtil.getProp("jdbc_username"), PropsUtil.getProp("jdbc_password"));
        } catch (ClassNotFoundException var2) {
            var2.printStackTrace();
        } catch (SQLException var3) {
            var3.printStackTrace();
        }

        return conn;
    }

    public List getTableColumnsByTableName(String dbName, String tableName) {
        Connection conn = getConnection();
        ArrayList returnList = new ArrayList();

        try {
            DatabaseMetaData dm = conn.getMetaData();
            ResultSet rs = null;

            try {
                rs = dm.getColumns(dbName, (String)null, tableName, "%");

                while(rs.next()) {
                    String column = rs.getString("COLUMN_NAME");
                    String descInfo = (String)rs.getObject(12);
                    String typeInt = rs.getString(5);
                    String type = (String)BeanUtil.constantCoumType.get(typeInt);
                    FieldBean bean = new FieldBean();
                    bean.setName(column);
                    bean.setType(type);
                    bean.setDescInfo(descInfo);
                    returnList.add(bean);
                }
            } finally {
                if(rs != null) {
                    rs.close();
                }

            }
        } catch (SQLException var16) {
            var16.printStackTrace();
        }

        return returnList;
    }

    public static void main(String[] args) throws Exception {
        JDBCUtil util = new JDBCUtil();
        List<FieldBean> list = util.getTableColumnsByTableName("framework", "tb_sys_menu");
        Iterator var4 = list.iterator();

        while(var4.hasNext()) {
            FieldBean bean = (FieldBean)var4.next();
            System.out.println(bean.getName());
        }

    }
}
